package com.qqbot.listener;

import com.alibaba.fastjson.JSONObject;
import com.qqbot.exception.BusinessException;
import com.qqbot.utils.HttpUtil;
import com.qqbot.vo.AudioVo;
import lombok.extern.slf4j.Slf4j;
import love.forte.simboot.annotation.Filter;
import love.forte.simboot.annotation.FilterValue;
import love.forte.simboot.annotation.Listener;
import love.forte.simbot.component.mirai.message.MiraiAudio;
import love.forte.simbot.component.mirai.message.MiraiSendOnlyAudio;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.event.FriendMessageEvent;
import love.forte.simbot.resources.Resource;
import net.mamoe.mirai.contact.AudioSupported;
import net.mamoe.mirai.internal.message.data.OnlineAudioImpl;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriUtils;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * @author 付亚全
 */
@Component
@Slf4j
public class PrivateListener {

    /**
     * 菜单
     *
     * @param event
     * @return
     */
    @Listener
    @Filter(value = "转语音/{{text}}")
    public EventResult coverAudio(FriendMessageEvent event, @FilterValue("text") String text) throws BusinessException, MalformedURLException, UnsupportedEncodingException {
        log.info("PrivateListener.coverAudio.params=====>sender:{},text:{}", event.getSource().getId(), text);
        String json = HttpUtil.httpGet(UriUtils.decode("https://xiaoapi.cn/API/tts.php?msg=" + text, "UTF-8").replace(" ", ""));
        AudioVo vo = JSONObject.parseObject(json, AudioVo.class);
        MiraiSendOnlyAudio audio = new MiraiSendOnlyAudio(Resource.of(new URL(vo.getData().getMp3()), text));
        event.replyBlocking(audio);
        return EventResult.truncate();
    }

}
